package kiwi.assist.dev.guieditor.command {
	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.event.GUIOperateEvent;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.util.SkinComponentAdapter;

	import org.robotlegs.mvcs.Command;

	import flash.display.DisplayObject;
	import flash.filesystem.File;
	import flash.utils.Dictionary;

	/**
	 * @author zhangming.luo
	 */
	public class NewProjectCommand extends Command {
		[Inject]
		public var detail : UiDetailVO;
		[Inject]
		public var config : GUIEditorConfig;

		override public function execute() : void {
			super.execute();
			detail.backgroundSkinList.forEach(removeMe);
			detail.backgroundSkinList = new Vector.<SkinComponentAdapter>();
			detail.uiList.forEach(removeMe);
			detail.uiList = new Vector.<DisplayObject>();
			detail.configList = new Dictionary();
			detail.container = null;
			removeMe(detail.containerImpl);
			detail.containerImpl = null;
			detail.currentEditUi = null;
			detail.name = "noname";
			detail.projectPath = File.desktopDirectory.nativePath;
			// 清空画布
			// 重置选择
			dispatch(new GUIOperateEvent(GUIOperateEvent.START_EDIT, null));
			// 选择默认容器
			if (config.defaultContainer) {
				dispatch(new GUIEditorEvent(GUIEditorEvent.SHOW_CONTAINER_SELECT_DIALOG, config.defaultContainer));
			}
		}

		private function removeMe(item : DisplayObject, index : int = 0, array : * = null) : void {
			if (!item || !item.parent) {
				return;
			}
			item.parent.removeChild(item);
		}
	}
}
